// @ts-ignore
import md5 from "js-md5";
// @ts-ignore
import useClipboard from "vue-clipboard3";
import router from "@/router";
import { createApp, h } from "vue";

// 计算MD5
export const MD5 = (data: any) => {
  return md5(data);
};
// 路由跳转
export const toView = (viewName: string, params?: any, query?: any): void => {
  router.push({ name: viewName, params, query });
};
// 复制
export const copyToBoard = (text: string) => {
  if (!text) return false;
  const { toClipboard } = useClipboard();
  const copyInfo = async (text: string) => {
    try {
      await toClipboard(text);
      return true;
    } catch (e) {
      return false;
    }
  };
  return copyInfo(text);
};
// 时间格式化
export const timeFormat = (timestamp: number | string | Date | null): string => {
  if (!timestamp) return "";
  if (typeof timestamp === "string") {
    timestamp = Number(timestamp);
  }
  if (timestamp.toString().length === 10) {
    // @ts-ignore
    timestamp = timestamp * 1000;
  }
  const date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const Y = date.getFullYear() + "-";
  const M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
  const D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  const h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  const m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) + ":";
  const s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
};
// 时间格式化 小时分钟
export const timeFormatHM = (timestamp: number | string | Date | null): string => {
  if (!timestamp) return "";
  if (typeof timestamp === "string") {
    timestamp = Number(timestamp);
  }
  if (timestamp.toString().length === 10) {
    // @ts-ignore
    timestamp = timestamp * 1000;
  }
  const date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
  const h = (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  const m = (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes());
  return h + m;
};
// 登出并清空localstorage
export const leaveOut = (): void => {
  localStorage.clear();
  location.reload();
};
// 现在的时间戳
export const getNow = () => {
  return new Date().getTime();
};
// 虚拟组件的高度获取
export const getComponentVirtualHeight = (comp: any, data: any, containerWidth: number): number => {
  const offScreenDom = document.getElementById("offscreen");
  if (offScreenDom && containerWidth) {
    offScreenDom.style.width = containerWidth + "px";
  }
  const newElement = h(comp, { data });
  const app = createApp({
    render() {
      return newElement;
    }
  });
  // 屏蔽警告信息
  app.config.warnHandler = () => null;
  
  const vm = app.mount("#offscreen");
  let height = vm.$el.offsetHeight;
  // 清除挂载的dom并unmount App
  while (offScreenDom?.firstChild) {
    offScreenDom.removeChild(offScreenDom.firstChild);
  }
  app.unmount();
  return height;
};
// 计算数字数组的累加和
export const calcListTotal = (arr: number[]) => {
  return arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
};
/**
 * 二分法计算index
 * @param arr
 * @param target
 */
export const findIndexByBinary = (arr: number[], target: number) => {
  let left = 0;
  let right = arr.length - 1;
  let index = -1;
  while (left <= right) {
    const mid = Math.floor((left + right) / 2);
    const sum = arr.slice(0, mid + 1).reduce((acc, cur) => acc + cur, 0);
    if (sum >= target) {
      index = mid;
      right = mid - 1;
    } else {
      left = mid + 1;
    }
  }
  return index;
};
